/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main.environment.loader;

import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.common.Tile;
import main.environment.IEnvironment;
import main.environment.LevelChange;
import main.environment.Location;

/**
 *
 * @author Fox
 */
public class EnvironmentLoader {
    
    private final BufferedImage imgBlock;
    private final BufferedImage imgFloor;
    private final IEnvironment environment;
    private final EnvironmentMappings map;
    
    private int x = 0;
    private int y = 0;
    
    public EnvironmentLoader(IEnvironment e, BufferedImage blocks, BufferedImage floors){
        imgBlock = blocks;
        imgFloor = floors;
        this.environment = e;
        map = new EnvironmentMappings();
    }
    
    public void changeLevel(LevelChange change){
        loadSquare(x+change.getX(),y+change.getY());
    }
    
    private void loadSquare(int x, int y) {
        this.x = x;
        this.y = y;
        
        try {
            for (int yy = 0; yy<environment.getHeight(); yy++){
                for (int xx = 0; xx<environment.getWidth(); xx++){
                    
                        Tile block = map.mapGet(imgBlock.getRGB((x*(environment.getWidth()-1))+xx,(y*(environment.getHeight()-1))+yy));
                        Tile floor = map.mapGet(imgFloor.getRGB((x*(environment.getWidth()-1))+xx,(y*(environment.getHeight()-1))+yy));
                        
                        environment.updateBlock(new Location(xx,yy,block));
                        environment.updateFloor(new Location(xx,yy,floor));
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(EnvironmentLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }
}
